<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 角色管理
 * @author www.dingwei.cn
 */
class Role extends MY_Controller {
    public $mRole=null; //临时定义一个 待删除

    public function __construct() {
        parent::__construct();
        $this->loadModel('AdmRoleModel', 'mRole');
    }
	/**
	 * 首页
	 */
	public function index(){
		$data = array('status' => $this->mRole->getStatus());
		$this->view('setting/role/home', $data,'datatable');
	}

    /**
     * 管理员编辑
     */
    public function edit(){
        if($_POST){
           if($this->_save()){
           		$this->$this->ajaxReturn(1, '已保存');
           }
           $this->$this->ajaxReturn(0, '保存失败');
        }
        
        $data = array(
        		'is_add' => false,
        		'info' => $this->mRole->getInfo(get('id')),
        		'menu' => getCustomConfig('menu', 'admin')
        );
        
        $this->view('setting/role/save', $data, 'validation,uniform');
    }
    
    /**
     * 保存数据
     */
    private function _save(){
   
    	$id = post('id');
    	$name = trim(post('name'));
    	$p = json_encode(post('p'));
    	if( ! $name){
    		$this->$this->ajaxReturn(0, '名称不能为空');
    	}
    	
    	if($id && ! is_numeric($id)){
    		$this->ajaxReturn(0, '参数错误');
    	}

    	if($id){
    		$info = $this->mRole->getInfo($id);
    		if( ! $info){
    			$this->ajaxReturn(0, '角色信息不存在');
    		}
    	}
    	
    	$data = array(
    				'name' => $name,
    				'permissions' => $p,
    	);

    	if($id){
    		$this->db->where('id', $id);
    		$query = $this->mRole->save($data, $id);
    	}else{
    		
    		$data['status'] = 2;
    		$data['addtime'] = time();
    		$query = $this->mRole->save($data);
    	}
    	
    	if($query){
    		$this->ajaxReturn(1, '已保存', null, getUrl('setting/role'));
    	}
    	
    	$this->ajaxReturn(0, '保存失败');
    }

    /**
     * 添加管理员
     */
    public function add(){
        if($_POST){
        	if($this->_save()){
        		$this->$this->ajaxReturn(1, '已保存');
        	}
        	$this->$this->ajaxReturn(0, '保存失败');
        }
        
        $data = array(
        			'is_add' => true,
        			'menu' => getCustomConfig('menu', 'admin')
        );
        
        $this->view('setting/role/save', $data,'validation,uniform');
    }
    
	/**
	 * 列表
	 */
	public function getList(){
        $data=$this->mRole->getList();
        echo json_encode($data);
    }

    /**
     * 删除
     */
    public function delete(){
        $id = get('id');

        $info = $this->mRole->getInfo($id);
        if (!$info){
            $this->ajaxReturn(0, '角色不存在');
        }
        //判断角色下是否存在管理员
        $this->db->select('id,name')
                ->where('role_id',$id);
        $adminInfo = $this->mUser->getAll($this->mUser->tbUser);
        if($adminInfo){
            $this->ajaxReturn(0, '请先删除角色下的管理员');
        }
        if($this->mRole->deleteRole($id)){
            $this->ajaxReturn(1, '删除成功');
        }

        $this->ajaxReturn(0, '删除失败');
    }

    /**
	 * 修改状态
	 */
    public function changeStatus(){
    	
        $id = get('id');
        $status = (int)get('status');
        if( ! in_array($status, array(1, 2))){
        	$this->ajaxReturn(0, '参数错误');
        }
        if($this->mRole->save(array('status' => $status), $id)){
        	$this->ajaxReturn(1, '已保存');
        }
        
        $this->ajaxReturn(0, '保存失败');
    }
}
